//
//  FKBankCardListVC.m
//  HappyTest
//
//  Created by Macbook Pro on 2019/2/12.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKBankCardListVC.h"
#import "FKAddBankCardVC.h"
#import "FKBankCardCell.h"

@interface FKBankCardListVC ()

@property (nonatomic, strong) NSArray *datas;
@property (nonatomic, strong) UIView *emptyView;

@end

@implementation FKBankCardListVC

- (NSArray *)datas
{
    if (!_datas) {
        _datas = [NSArray array];
    }
    return _datas;
}


- (UIView *)emptyView
{
    if (!_emptyView) {
        _emptyView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, fkScreenW, 200)];
//        _emptyView.backgroundColor = [UIColor redColor];
        
        UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"empty_bank"]];
        [_emptyView addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.center.offset(0);
            make.centerX.offset(0);
            make.centerY.offset(fk_adjustW(-50));
            make.width.mas_equalTo(fk_adjustW(imageView.image.size.width));
            make.height.mas_equalTo(fk_adjustW(imageView.image.size.height));
        }];
        
//        UILabel *label = [[UILabel alloc] init];
//        [_emptyView addSubview:label];
//        label.font = fk_adjustFont(16);
//        label.text = @"还没有订单，快去逛逛吧!";
//        label.textColor = fkColor999999;
//        [label mas_makeConstraints:^(MASConstraintMaker *make) {
//            make.top.equalTo(imageView.mas_bottom).offset(20);
//            make.centerX.offset(0);
//        }];
        
        UIButton *addBtn = [UIButton fk_btnWithTarget:self action:@selector(addBankCard) font:fk_adjustFont(15) normalTitle:@"添加银行卡" normalTitleColor:[UIColor whiteColor] selectedTitle:nil selectedTitleColor:nil normalImg:nil selectedImg:nil bgImg:[UIImage imageNamed:@"bg_main"] selectedBgImg:nil];
        [_emptyView addSubview:addBtn];
        [addBtn fk_viewCornerRadius:5 borderWidth:0 borderColor:nil];
        [addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(imageView.mas_bottom).offset(20);
            make.centerX.offset(0);
            make.width.mas_equalTo(fk_adjustW(225));
            make.height.mas_equalTo(fk_adjustW(40));
        }];
    }
    return _emptyView;
}

- (instancetype)initWithStyle:(UITableViewStyle)style
{
    return [super initWithStyle:UITableViewStyleGrouped];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"我的银行卡";
//    self.navigationItem.rightBarButtonItem = [UIBarButtonItem fk_itemWithImageName:@"card_add-" highImageName:@"card_add-" target:self action:@selector(addBankCard)];
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem fk_itemWithTarget:self action:@selector(addBankCard) text:@"添加" textColor:fkColor333333 font:fk_adjustFont(16)];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadData)];
    [self loadData];
}

- (void)addBankCard
{
    FKAddBankCardVC *vc = [[FKAddBankCardVC alloc] init];
    fkPushNav(vc);
    fkWeakSelf(self);
    vc.addBankCardSuccess = ^{
        [weakself loadData];
    };
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.datas.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    FKBankCardCell *cell = [FKBankCardCell cellWith:tableView];
    cell.data = self.datas[indexPath.section];
    fkWeakSelf(self);
    cell.unBindBlock = ^(NSDictionary * _Nonnull bankCard) {
        [weakself fk_alertWithTitle:@"确定要解除绑定吗?" confirm:^{
            [weakself unBindBankCard:bankCard[@"id"]];
        }];
    };
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return fk_adjustW(86);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [UIView new];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}

- (void)loadData
{
    [SVProgressHUD show];
    [FKHttpTool postWithSuffix:@"m=Home&c=Bank&a=index" param:nil success:^(FKBaseResult *result) {
        [self.tableView.mj_header endRefreshing];
        if (result.isSuccess) {
            [SVProgressHUD dismiss];
            self.datas = result.data[@"bank_info"];
            [self.tableView reloadData];
        }
        UIButton *rightItem = self.navigationItem.rightBarButtonItem.customView;
        rightItem.hidden = self.datas.count == 0;
        [self.tableView fk_tableViewCheckEmptyDataWithDataCount:self.datas.count emptyView:self.emptyView];
    } failure:^(NSError *error) {
        [self.tableView.mj_header endRefreshing];
        UIButton *rightItem = self.navigationItem.rightBarButtonItem.customView;
        rightItem.hidden = self.datas.count == 0;
        [self.tableView fk_tableViewCheckEmptyDataWithDataCount:self.datas.count emptyView:self.emptyView];
    }];
}

- (void)unBindBankCard:(NSString *)cardId
{
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"bank_card_id"] = cardId;
    [FKHttpTool postWithSuffix:@"m=Home&c=Bank&a=untieBankCard" param:param success:^(FKBaseResult *result) {
        if (result.isSuccess) {
            [SVProgressHUD showSuccessWithStatus:@"解绑成功"];
            [self loadData];
        }
    } failure:nil];
}

@end
